package edu.bit.water.service;

import edu.bit.water.dao.EntitySensorDao;
import edu.bit.water.dao.EvaWaterSensorInfoDao;
import edu.bit.water.entity.EntitySensor;
import edu.bit.water.entity.EvaWaterSensorInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @Description: watersensor的service层
 * @Author: YYF
 */
@Service
public class EvaWaterSensorInfoService {
    @Autowired
    private EvaWaterSensorInfoDao evaWaterSensorInfoDao;
    @Autowired
    private EntitySensorDao entitySensor;

    public List<EvaWaterSensorInfo> queryEvaWaterSensorInfo(int company_id,int currentUserId) {
        return evaWaterSensorInfoDao.queryEvaWaterSensorInfoByCompanyId(company_id,currentUserId);
    }

    public void deleteEvaWaterSensorInfo(String idList) {
        evaWaterSensorInfoDao.deleteEvaWaterSensorInfo(idList);
    }

    public List<Map<String, Object>> selectEntitySensorIdAndName(){
        return entitySensor.selectEntitySensorIdAndName();
    }

    public Map<String, Object> selectEntitySensorById(int id){
        return entitySensor.selectEntitySensorById(id);
    }

    public Map<String, Object> insertEvaWaterSensorInfo(EvaWaterSensorInfo evaWaterSensorInfo){
        Map<String,Object> modelMap = new HashMap<>();
        try {
            evaWaterSensorInfoDao.insertEvaWaterSensorInfo(evaWaterSensorInfo);
            modelMap.put("status","success");
        }catch (DataIntegrityViolationException e){
            modelMap.put("status","failed");
            modelMap.put("msg","水表编号不存在");
        }
        return modelMap;
    }

    public Map<String, Object> updateEvaWaterSensorInfo(EvaWaterSensorInfo evaWaterSensorInfo){
        Map<String,Object> modelMap = new HashMap<>();
        try {
            evaWaterSensorInfoDao.updateEvaWaterSensorInfo(evaWaterSensorInfo);
            modelMap.put("status","success");
        }catch (DataIntegrityViolationException e){
            modelMap.put("status","failed");
            modelMap.put("msg","水表编号不存在");
        }
        return modelMap;
    }
    public int query_entity_sensor_cid(int testId) {
        return evaWaterSensorInfoDao.query_entity_sensor_cid(testId);
    }
}
